package com.aiinterview.subjectservice.controller.user;

import com.aiinterview.common.entity.Result;
import com.aiinterview.subjectservice.file.service.impl.MinIOFileStorageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserInfoController {

    @Autowired
    private MinIOFileStorageService minIOFileStorageService;

    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file){
        try
        {
            String oldFileName = file.getOriginalFilename();
            String postFix = oldFileName.substring(oldFileName.lastIndexOf("."));
            String fileName = UUID.randomUUID().toString().replace("-", "");
            String fileUrl = null;
            fileUrl = minIOFileStorageService.uploadImgFile("", fileName+postFix, file.getInputStream());
            log.info("上传图片成功，fileUrl:{}",fileUrl);
            return Result.ok(fileUrl);
        }
        catch (Exception e)
        {
            return Result.fail("上传图片失败");
        }
    }

}
